import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * Created by HP on 2018/10/2.
 */
public class CharsetFilter implements Filter {
    private String charsetcode="UTF-8";
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request=(HttpServletRequest)req;
        HttpServletResponse response=(HttpServletResponse)resp;
        request.setCharacterEncoding(charsetcode);
       response.setContentType("html/text; charset="+charsetcode+"");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {
        charsetcode = config.getInitParameter("charsetcode");
    }

}
